package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.entity.Project;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.mapper.ProjectMapper;
import com.siwei.mes.service.ProjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 砼行工程表(Project)表服务实现类
 *
 * @author makejava
 * @since 2023-11-30 15:44:59
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Long create(Project entity) {
        entity.setProjectNumber(UidUtils.genNo(GenerateCodeEnum.PROJECT.getCode()));
        entity.setAddTime(new Date());
        projectMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Project entity) {
        Project project = projectMapper.selectByPrimaryKey(entity.getId());
        assertTrue(project == null, "砼行工程表信息不存在");
        projectMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Project project = projectMapper.selectByPrimaryKey(id);
        assertTrue(project == null, "砼行工程表信息不存在");
        projectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Project> getPage(PageQuery<Project, Project> query) {
        Project searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> projectMapper.getList(searchParam));
    }

    @Override
    public Project loadById(Long id) {
        Project project = projectMapper.selectByPrimaryKey(id);
        Map params = new HashMap();
        params.put("projectName", project.getProjectName());
        project.setExperimentCount(projectMapper.experimentCount(params));
        params.put("isQualified", "2");
        project.setExperimentExpCount(projectMapper.experimentCount(params));
        return project;
    }

    @Override
    public List<Project> getAll(Project entity) {
        return projectMapper.getList(entity);
    }
}
